//--------------------------------------------------------------------------------
// This file is a portion of the D2D1Renderer.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Mirco Timmermann
//
//--------------------------------------------------------------------------------
#pragma once

#include "..\DisplayObject.h"
#include "..\..\..\Rendering\canvas\Color.h"
#include "..\..\..\Utility\KeyString.h"

namespace D2 {

class TextField : public DisplayObject {
public:
	TextField(int x, int y, int width, int height);
	~TextField(void);

	virtual bool Draw(Direct2DContext* ctx, D2D1::Matrix3x2F* mtx = 0);

	void DrawTextArea(Direct2DContext* ctx);
	void DrawTextPointer(Direct2DContext* ctx);

//
	virtual void OnKeyDown(const KeyboardEvent &e);

	virtual void OnMouseDown0(const MouseEvent &e);
	virtual void OnMouseDrag0(const MouseEvent &e);

	virtual void OnFocusLost(const MouseEvent &e); //TODO: MouseEvent!? -> Event wohl eher

private:
	int _pointerCounter; //for flashing the textpointer.
	
	int _textIndexIStart; //current char index from the text where the pointer will be shown.
    int _textIndexIEnd;

	float _charWidth;
	float _charHeight;

	float _totalWidth;
	float _totalHeight;

	bool _maxInputStop; //stops the input if the length of words are reached the end of the textfield. 
	
	bool _focus;

public:
	bool selectable;

private:
	int GetCharIndex(float x);

private:
	std::string _text;

public:
	void setTextPointer(int index);
	void setTextPointerToEnd();
	void SetText(std::string text);
	void AppendText(std::string text);

public:
	std::string GetText();

public:
	bool _writeable;

private:
	Color _fg;
	Color _bg;

public:
	void SetFgColor(float r, float g, float b);
	void SetBgColor(float r, float g, float b);

	float GetTotalWidth();
	float GetTotalHeight();

public:
	bool showBackground;

private:
	KeyString _keyString;

};

}